package com.cz.kingdee.prod;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.cz.kingdee.KingdeeClient;
import com.cz.kingdee.dto.PrdInstock;
import com.cz.kingdee.dto.PrdMo;
import com.cz.kingdee.dto.PrdMorpt;
import com.kingdee.bos.webapi.entity.QueryParam;
import com.kingdee.bos.webapi.sdk.K3CloudApi;

import java.util.ArrayList;
import java.util.List;

/**
 * PRD_MO 生产订单
 * 查询缺失的生产汇报单子
 */
public class PrdMoMain {
    public static void main(String[] args) throws Exception {

        KingdeeClient client = new KingdeeClient();
        K3CloudApi k3CloudApi = client.getProdKingdeeClient();
        QueryParam queryParam = new QueryParam();
        queryParam.setFormId("PRD_MO");
        queryParam.setFieldKeys("FBillNo,FStatus,FTreeEntity_FEntryId,FTreeEntity_FSeq");

        //queryParam.setStartRow(1);
        //queryParam.setLimit(2);
        //FA
       /* queryParam.setFilterString("FPrdOrgId = '100034' and FWorkShopID = '2539006' and  FDate >= '2025-01-01 00:00:00'  and FDate < '2025-02-01 00:00:00'" +
                " and FStatus = 5");
        */

        //zj 组件
        queryParam.setFilterString("FPrdOrgId = '100034' and FWorkShopID = '29005174' and  FDate >= '2025-01-01 00:00:00'  and FDate < '2025-02-01 00:00:00'" +
                " and FStatus = 5");


        List<PrdMo> prdMos = k3CloudApi.executeBillQuery(queryParam, PrdMo.class);

        System.out.println("prdMos size: " + prdMos.size());

        List<PrdMo> notExistPrdMos = new ArrayList<>();
        for (PrdMo prdMo : prdMos) {
            String fBillNo = prdMo.getFBillNo();
            int orderFEntryId = prdMo.getFTreeEntity_FEntryId();
            int orderFSeq = prdMo.getFTreeEntity_FSeq();

            QueryParam queryParam2 = new QueryParam();
            queryParam2.setFormId("PRD_MORPT");
            queryParam2.setFieldKeys("FMoBillNo,FMoEntrySeq");
            //FA
            queryParam2.setFilterString("FMoEntryId ='" + orderFEntryId + "'");
            List<PrdMorpt> prdMorptList = k3CloudApi.executeBillQuery(queryParam2, PrdMorpt.class);

            if (CollUtil.isEmpty(prdMorptList)) {
                notExistPrdMos.add(prdMo);
            } else {
                System.out.println(fBillNo + " " + orderFEntryId + " " + orderFSeq+ " "+prdMorptList.size());
            }
        }

        List<PrdMo> noPrdMoList = new ArrayList<>();
        if(CollUtil.isNotEmpty(notExistPrdMos)){
            for(PrdMo prdMo:notExistPrdMos){
                int orderFEntryId = prdMo.getFTreeEntity_FEntryId();
                QueryParam queryParam3 = new QueryParam();
                queryParam3.setFormId("PRD_INSTOCK");
                queryParam3.setFieldKeys("FMaterialId,FMaterialName,FBillNo,FDate,FMoBillNo,FMoId,FMoEntryId,FRealQty,F_ora_XSDDH,FMoEntrySeq,FMaterialId.FNumber,FApproveDate,FPrdOrgId.FName,FWorkShopId1.FName,FStockId.FName");
                //FA
                queryParam3.setFilterString("FMoEntryId =" + orderFEntryId + " and FDate >= '2025-01-01 00:00:00'  and FDate < '2025-02-01 00:00:00'");
                List<PrdInstock> prdInstockList = k3CloudApi.executeBillQuery(queryParam3, PrdInstock.class);
                if(CollUtil.isNotEmpty(prdInstockList)){
                    noPrdMoList.add(prdMo);
                }
            }
        }

        System.out.println("noPrdMoList size: " + JSONUtil.toJsonStr(noPrdMoList));
    }
}
